<?php
/*
 * Copyright notice
 * 
 * (c) 2009 MH-Dev.de, Martin Hesse <info@mh-dev.de>
 * 
 * This script is part of the MH-Dev.-Board project. The MH-Dev.-Board 
 * is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU 
 * General Public License along with this program; if not, 
 * see http://www.gnu.org/licenses/.
 */

/* Include Geshi Syntax-Highlighting */
require_once Zend_Registry::get('config')->pubPath . '/geshi/geshi.php';


function bbcode_highlight($code, $lang)
{
	$code	= html_entity_decode($code, ENT_QUOTES, 'UTF-8');
	
	$geshi	= new GeSHi($code, $lang);
	$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 10);
	$geshi->set_encoding('utf-8');
	#$geshi->enable_strict_mode(GESHI_ALWAYS);
	$geshi->set_header_type(GESHI_HEADER_DIV);
	#$geshi->set_header_content_style('font-size:1.3em;');
	$geshi->set_header_content(Zend_Registry::get('lang')->_('bbcode_code_header'));
	$geshi->set_footer_content(Zend_Registry::get('lang')->_('bbcode_code_footer'));
	$geshi->set_overall_class('bbcode_code');
	
	return $geshi->parse_code();
}
			
/**
 * View_Helper BBCode-Parser
 * 
 * @author	Martin Hesse <info@mh-dev.de>
 * @package	MH-Dev.-Board
 */
class MHDEV_View_Helper_BBCode extends Zend_View_Helper_Abstract
{
	/*
	 * Parse BBCode
	 * 
	 * @param string $str text to parse
	 * 
	 * @return string $str parsed text
	 */
    public function BBCode($str, $smilies = true)
    {
    	
    	// Helper
    	$baseurl 	= new MHDEV_View_Helper_BaseUrl();
    	$getText	= new MHDEV_View_Helper_GetPostText();

    	// parse ...
		$str = trim($str);

		$bbcodes = array(
			'b'			=> array(1,'<strong>${1}</strong>'),
			'i'			=> array(1,'<i>${1}</i>'),
			'u'			=> array(1,'<span style="text-decoration:underline;">${1}</span>'),
			's'			=> array(1,'<strike>${1}</strike>'),
			'p'			=> array(1,'<p>${1}</p>'),
			'q'			=> array(1,'<q>${1}</q>'),
			'left'		=> array(1,'<div class="left">${1}</div>'),
			'center'	=> array(1,'<div class="center">${1}</div>'),
			'right'		=> array(1,'<div class="right">${1}</div>'),
			'img'		=> array(1,'<img src="${1}" />'),
			'info'   	=> array(1,'<p class="notice">${1}</p>'),
			'url' 		=> array(2,'<a target="_blank" href="${1}">${2}</a>'),
			'size' 		=> array(2,'<span style="font-size:${1}px">${2}</span>'),
			'youtube'	=> array(1, '<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/${1}&hl=de&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/${1}&hl=de&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>'),
		);

		while (list($trigger,$stuff) = each($bbcodes)) 
		{
			$re_str = '';
			switch ($stuff[0]) 
			{
				case 1: $re_str = "#\[$trigger\](.*?)\[/$trigger\]#is"; break;
				case 2: $re_str = "#\[$trigger=([^]]+?)\](.*?)\[/$trigger\]#is"; break;
				case 3: $re_str = "#\[$trigger=([^],]+?),([^]]+?)\](.*?)\[/$trigger\]#is"; break;
			}
			if ($re_str)
			while (preg_match($re_str,$str))
			$str = preg_replace($re_str,$stuff[1],$str);
		}
		
		// quote
		if(preg_match("#\[quote\](.*?)\[/quote\]#is", $str))
		{
			$str	= preg_replace("/\[quote\](.*?)\[\/quote\]/ies", "\$getText->GetPostText('\\1', true)", $str);
		}
		
		// code
		if(preg_match("#\[code=([^]]+?)\](.*?)\[\/code\]#is", $str))
		{
			$str	= preg_replace("/\[code=([^]]+?)\](.*?)\[\/code\]/ies", "bbcode_highlight('\\2','\\1')", $str);
		}

		// code 2
    	if(preg_match("#\[code\](.*?)\[\/code\]#is", $str))
		{
			$str	= preg_replace("/\[code\](.*?)\[\/code\]/ies", "bbcode_highlight('\\1','text')", $str);
		}
		
		// smilies
		if($smilies)
		{
			$dbSmilies = new Smilies();
			
			$getSmilies = $dbSmilies->fetchAll();
			
			if($getSmilies->count() > 0)
			{
				$resSmilies = $getSmilies->toArray();
				
				foreach($resSmilies AS $smiley)
				{
					$image	= '<img alt="' . $smiley['desc'] . '" title="' . $smiley['desc'] . '" src="' . $baseurl->BaseUrl() . '/static/gfx/smilies/' . $smiley['image'] . '" />';
					$str 	= str_replace($smiley['wrapper'], $image, $str); 
				}
			}
		}

		$str = preg_replace("#([\n ])([a-z]+?)://([^, \n\r]+)#i", "\\1<a href=\"\\2://\\3\" target=\"_blank\">\\2://\\3</a>", $str);

		$str = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^, \n\r]*)?)#i", "\\1<a href=\"http://www.\\2.\\3\\4\" target=\"_blank\">www.\\2.\\3\\4</a>", $str);

		$str = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([^, \n\r]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $str);

		$str = preg_replace('=\[color\s*\=\s*(#[0-9a-f]{6}|[a-z]+)\s*\]\s*(.*?)\s*\[/color\]=si', '<span style="color:$1">$2</span>', $str);	

		$str = preg_replace("=<br(>|([\s/][^>]*)>)\r?\n?=i", "\n", $str);

        return $str;
    }  
}
?>